package com.Dao;

import com.bean.User;
import com.tools.DButil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class UserDao {

    //添加用户
    public void adduser(User user) throws SQLException {
        String sql="insert into user(username,password,nick,sex,mood)values(?,?,?,?,?)";//id会自增
        QueryRunner runner=new QueryRunner(DButil.getDataSource());
        int row=runner.update(sql,user.getUsername(),user.getPassword(),user.getNick(),user.getSex(),user.getMood());
        if(row==0){
            throw new RuntimeException();
        }
    }

    //查询用户
    public User findUserByNameandPwd(String username,String password)throws SQLException{
        String sql="select * from user where username=? and password=?";
        QueryRunner runner=new QueryRunner( DButil.getDataSource());
        return runner.query(sql, new BeanHandler<User>(User.class),username,password);
    }
    //修改用户信息
    public void updateUer(User user) throws SQLException{  //根据user表格的id取修改
        String sql="update user set password=?,nick=?,mood=?,head=? where username=?";
        QueryRunner runner=new QueryRunner(DButil.getDataSource());
        runner.update(sql, user.getPassword(), user.getNick(),user.getMood(),user.getHead(),user.getUsername());

    }

    //查询博主
    public User queryBlogger()throws SQLException{
        String sql="select * from user";
        QueryRunner runner=new QueryRunner( DButil.getDataSource());
        return runner.query(sql, new BeanHandler<User>(User.class));
    }
}
